package com.wenda.admin.core;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 

import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
import jpcap.NetworkInterfaceAddress;
import org.apache.commons.lang3.RandomStringUtils;

public class ScanNetworkCard {
 
    public static List<Map<String, Object>> list = new ArrayList<>();
    /**
     * 扫描出所有的网卡信息
     */
    public static List scan() {
        //封装的所有网卡信息
        try{
            NetworkInterface[] devices = JpcapCaptor.getDeviceList();
            for (int i = 0; i < devices.length; i++) {
                Map<String, Object> networkCardMap = new HashMap<>();
                networkCardMap.put("id", RandomStringUtils.randomNumeric(8));   //id
                networkCardMap.put("netcd_no", i);   //网卡在系统中的序列
                networkCardMap.put("netcd_name", devices[i].name); //网卡名
                networkCardMap.put("netcd_description", devices[i].description); //网卡名
                NetworkInterfaceAddress[] addresses = devices[i].addresses;
                networkCardMap.put("netcd_datalink_name", devices[i].datalink_name); //数据链路名称
                networkCardMap.put("netcd_datalink_description", devices[i].datalink_description); //数据链路描述
                for (int j = 0; j < addresses.length; j++) {
                    if(j == 0) {
                        networkCardMap.put("netcd_Iipv6", addresses[j].address.toString());   //ipv6
                    } else if(j == 1) {
                        networkCardMap.put("netcd_ipv4", addresses[j].address.toString()); //ipv4
                        if(addresses[j].broadcast != null){
                            networkCardMap.put("netcd_broadcast", addresses[j].broadcast.toString()); //广播
                        }
                        if(addresses[j].subnet != null){
                            networkCardMap.put("netcd_subnet", addresses[j].subnet.toString()); //子网掩码
                        }
                    }
                }
                int length = devices[i].mac_address.length;  
                int count = 1; 
                StringBuilder sb = new StringBuilder("");
                for (byte b : devices[i].mac_address) {  
                    sb.append(Integer.toHexString(b & 0xff));
                    if(count++ != length) 
                        sb.append(":");
                }
                networkCardMap.put("netcd_mac", sb.toString());   //mac地址
                //把所有的设备信息存入到
                networkCardMap.put("dev", devices[i]);
                list.add(networkCardMap);
            }
            return list;
        }catch(Exception e){
            e.printStackTrace();
        }
        return list;
    }
 
 
    public static void main(String[] args) {
 
        List scan = scan();
        System.out.println(scan.size());
        for (int i = 0; i < scan.size(); i++) {
            Iterator<Map.Entry<String, Object>> it = ((Map<String, Object>) scan.get(i)).entrySet().iterator();
             while (it.hasNext()) {
                   Map.Entry<String, Object> entry = it.next();
                   System.out.println("[key]= " + entry.getKey() + "[value]= " + entry.getValue());
                }
             System.out.println("*********************************************************");
        }
 
    }
 
}